<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MatchingData extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {
        $this->load->library('table');
        if (helper_checkUserLogin($this->facebook, $this->config->config)) {
            
        }
    }
    
    private function getDataLikes() {
        $LikesArray = array();
        $fql = "SELECT books,movies,music,uid,sex,birthday_date,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
        $param = array(
            'method' => 'fql.query',
            'query' => $fql,
            'callback' => ''
        );
        $LikesArray = $this->facebook->api($param);
        return $LikesArray;
    }

    private function getUserLikes() {
        return $this->facebook->api('/me?fields=id,books,music,movies,name,gender,birthday');
    }

    private function getAge($birthday_date) {
        return floor((time() - strtotime($birthday_date)) / (60 * 60 * 24 * 365));
    }

    private function processMatching() {
        $PointsArray = array();
        $FriendInfo = array();
        $UserId = $this->facebook->getUser();
        $FriendLikes = $this->getDataLikes();
        $UserLikes = $this->getUserLikes();
        //print_r($UserLikes);
        $UserBooks = $UserLikes['books']['data'];
        $UserMovies = $UserLikes['movies']['data'];
        $UserMusics = $UserLikes['music']['data'];
        $this->load->view('header');
        //  echo $CurrentDay;
        foreach ($FriendLikes as $index => $friend) {
            if ($index == 0) {
                continue;
            } // dont check the first user
            $PointsArray[$friend['uid']] = 0;
            $FriendInfo[$friend['uid']] = array();
            $FriendInfo[$friend['uid']]['sex'] = $friend['sex'];
            $FriendInfo[$friend['uid']]['name'] = $friend['name'];
            $FriendInfo[$friend['uid']]['books'] = "";
            $FriendInfo[$friend['uid']]['movies'] = "";
            $FriendInfo[$friend['uid']]['music'] = "";
            $numberCate = 0;
            //matching books
            if (count($UserBooks) > 0 && $friend['books'] != "") {
                $numberCate++;
                foreach ($UserBooks as $item) {
                    if (strpos($friend['books'], $item['name'])) {
                        $FriendInfo[$friend['uid']]['books'] .= $item['name'] . ",";
                        if($item['category']=="Book"){
                             $PointsArray[$friend['uid']]++;
                        }
                        else{
                            $PointsArray[$friend['uid']]+=0.2;
                        }
                    };
                }
            }
            //matching movies

            if (count($UserMovies) > 0 && $friend['movies'] != "") {
                $numberCate++;
                foreach ($UserMovies as $item) {
                    if (strpos($friend['movies'], $item['name'])) {
                        $FriendInfo[$friend['uid']]['movies'] .= $item['name'] . ",";
                        $PointsArray[$friend['uid']]++;
                    };
                }
            }
            //matching music
            if (count($UserMusics) > 0 && $friend['music'] != "") {
                $numberCate++;
                foreach ($UserMusics as $item) {
                    //print_r($item);
                    if (strpos($friend['music'], $item['name'])) {
                        $FriendInfo[$friend['uid']]['music'] .= $item['name'] . ",";
                        $PointsArray[$friend['uid']]++;
                    };
                }
            }

            //check number cate matching
            if ($numberCate == 2) {
                $PointsArray[$friend['uid']] *=1.5;
            }
            if ($numberCate == 3) {
                $PointsArray[$friend['uid']] *=2;
            }

            //check sex matching
            if ($UserLikes['gender'] == $friend['sex']) {
                $PointsArray[$friend['uid']] *=1.5;
            } else {
                if ($friend['sex'] != "") {
                    $PointsArray[$friend['uid']] *=1.2;
                }
            }
            //check age matching
            $UserAge = $this->getAge($UserLikes['birthday']);
            if ($friend['birthday_date'] != "") {
                $friendAge = $this->getAge($friend['birthday_date']);
                if ($friendAge > 0) {
                    $FriendInfo[$friend['uid']]['age'] = $friendAge;
                    $rangeAge = abs($UserAge - $friendAge);
                    if ($rangeAge <= 10) {
                        $PointsArray[$friend['uid']]*=1.5;
                    } else {
                        $PointsArray[$friend['uid']]*=1.2;
                    }
                }
                else{
                     $FriendInfo[$friend['uid']]['age'] = "";
                }
            } else {
                $FriendInfo[$friend['uid']]['age'] = "";
            }
        }
        arsort($PointsArray);
        $this->load->view('matchingdata', array("PointsArray" => $PointsArray, "FriendInfo" => $FriendInfo));
        $this->load->view('footer');
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */